<?php
namespace Home\Model;
use  Think\Model;
class CategoryBaseModel extends Model{

    protected $autoCheckFields =false;
   /**
     * 获得指定分类同级的所有分类以及该分类下的子分类
     *
     * @access  public
     * @param   integer     $cat_id     分类编号
     * @return  array
     */
   public function get_categories_tree($cat_id = 0) {

        $DB_PREFIX=C('DB_PREFIX');
        if ($cat_id > 0) {
            $sql = 'SELECT parent_id FROM ' . $DB_PREFIX . "category WHERE cat_id = '$cat_id'";
            $list = M()->query($sql);
            $result=$list[0];

            $parent_id = $result['parent_id'];
        } else {
            $parent_id = 0;
        }

        /*
          判断当前分类中全是是否是底级分类，
          如果是取出底级分类上级分类，
          如果不是取当前分类及其下的子分类
         */
        $sql = 'SELECT count(*) FROM ' . $DB_PREFIX . "category WHERE parent_id = '$parent_id' AND is_show = 1 ";
        if ( M()->query($sql) || $parent_id == 0) {
            /* 获取当前分类及其子分类 */
            $sql = 'SELECT c.cat_id,c.cat_name,c.parent_id,c.is_show,t.cat_image,t.cat_image_nav ' .
                    'FROM ' . $DB_PREFIX . 'category as c ' .
                    'left join ' . $DB_PREFIX. 'touch_category as t on t.cat_id = c.cat_id ' .
                    "WHERE c.parent_id = '$parent_id' AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";

            $res = M()->query($sql);
            
    

            foreach ($res AS $row) {

                if ($row['is_show']) {
                    $cat_arr[$row['cat_id']]['id'] = $row['cat_id'];
                    $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
                    $cat_arr[$row['cat_id']]['cat_image'] = get_image_mobile_path($row['cat_image']);
                    // $cat_arr[$row['cat_id']]['cat_image_nav'] = get_image_path(0, $row['cat_image_nav'],false);
                    $cat_arr[$row['cat_id']]['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('category/index', array('id' => $row['cat_id']));

                    if (isset($row['cat_id']) != NULL) {
                        $cat_arr[$row['cat_id']]['cat_id'] = $this->get_child_tree($row['cat_id']);
                    }
                }
            }
        }
        if (isset($cat_arr)) {
            return $cat_arr;
        }
    }
 public function get_child_tree($tree_id = 0) {
      $DB_PREFIX=C('DB_PREFIX');


        $three_arr = array();
        $sql = 'SELECT count(*) FROM ' .$DB_PREFIX . "category WHERE parent_id = '$tree_id' AND is_show = 1 ";
        if (M()->query($sql) || $tree_id == 0) {
            $child_sql = 'SELECT c.cat_id, c.cat_name, c.parent_id, c.is_show, t.cat_image,t.cat_image_nav ' .
                    'FROM ' . $DB_PREFIX . 'category as c ' .
                    'left join ' . $DB_PREFIX . 'touch_category as t on t.cat_id = c.cat_id ' .
                    "WHERE c.parent_id = '$tree_id' AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";
            $res = M()->query($child_sql);
           

            foreach ($res AS $row) {

                if ($row['is_show'])
                    $three_arr[$row['cat_id']]['id'] = $row['cat_id'];
                $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
                $three_arr[$row['cat_id']]['cat_image'] = get_image_mobile_path($row['cat_image']);
                $three_arr[$row['cat_id']]['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('category/index', array('id' => $row['cat_id']));

                if (isset($row['cat_id']) != NULL) {
                    $three_arr[$row['cat_id']]['cat_id'] = $this->get_child_tree($row['cat_id']);
                }
            }
        }
        return $three_arr;
    }

//获取下一级商品类别
public function get_one_child_tree($tree_id = 0) {
      $DB_PREFIX=C('DB_PREFIX');


        $three_arr = array();
        $sql = 'SELECT count(*) FROM ' .$DB_PREFIX . "category WHERE parent_id = '$tree_id' AND is_show = 1 ";
        if (M()->query($sql) || $tree_id == 0) {
            $child_sql = 'SELECT c.cat_id, c.cat_name, c.parent_id, c.is_show, t.cat_image ' .
                    'FROM ' . $DB_PREFIX . 'category as c ' .
                    'left join ' . $DB_PREFIX . 'touch_category as t on t.cat_id = c.cat_id ' .
                    "WHERE c.parent_id = '$tree_id' AND c.is_show = 1 ORDER BY c.sort_order ASC, c.cat_id ASC";
            $res = M()->query($child_sql);
           

            foreach ($res AS $row) {

                if ($row['is_show'])
                    $three_arr[$row['cat_id']]['id'] = $row['cat_id'];
                $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
                // $three_arr[$row['cat_id']]['cat_image'] = get_image_mobile_path(0,$row['cat_image']);
                $three_arr[$row['cat_id']]['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('category/index', array('id' => $row['cat_id']));
             
            }
        }
        return $three_arr;
    }


}
?>
